﻿using QuickChat_Service.Repositories.Database;
using QuickChat_Service.Repositories.Repositories.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace QuickChat_Service.Repositories.Repositories.Fakes
{
    public class FakeMessageRepository : FakeRepository<Message>, IMessageRepository
    {
        List<ChatSession> sessions { get; set; }

        public FakeMessageRepository()
        {
            this.DbSet.Add(new Message() { MessageId = 1, MessageText = "Message1" });
            this.DbSet.Add(new Message() { MessageId = 2, MessageText = "Message2" });
            this.DbSet.Add(new Message() { MessageId = 3, MessageText = "Message3" });
            this.DbSet.Add(new Message() { MessageId = 4, MessageText = "Message4" });
            this.DbSet.Add(new Message() { MessageId = 5, MessageText = "Message5" });

            ChatSession session1 = new ChatSession();
            session1.SessionId = 1;
            session1.Messages.Add(GetById(0));
            session1.Messages.Add(GetById(1));
            session1.Messages.Add(GetById(2));

            ChatSession session2 = new ChatSession();
            session2.SessionId = 2;
            session2.Messages.Add(GetById(3));
            session2.Messages.Add(GetById(4));

            sessions = new List<ChatSession>();

            sessions.Add(session1);
            sessions.Add(session2);
        }

        public IQueryable<Message> GetMessagesInSession(int sessionId)
        {
            return sessions.SingleOrDefault(sess => sess.SessionId == sessionId).Messages.AsQueryable();
        }

        public void SendMessage(string message, int sendersId, int sessionId)
        {
            this.DbSet.Add(new Message() 
            {
                MessageId = GetAll().Count(),
                MessageText = message,
                SendersId = sendersId,
                SessionId = sessionId
            });
        }
    }
}